<template>
  <div>
    <simple-table ref="multipleTable" :tableConfig="tableConfig" :tableData="tableData" @handleSelectionChange="handleSelectionChange"></simple-table>
  </div>
</template>
<script>
import simpleTable from '@/components/simpleTable.vue'
import {APISINGLELIST} from '@/common/api.js'
export default {
  props: ['singleList'],
  components: {simpleTable},
  data () {
    return {
      tableData: [],
      totalNumber: Number,
      tableConfig: {
        load: false,
        size: 'mini', // 表格大小
        style: 'width:100%', // 表格宽度
        align: 'center',
        border: true,
        maxHeight: 250,
        config: [
          {type: 'selection', width: '60px'},
          {name: '序号', type: 'index', width: '60px'},
          {id: 'student_title', name: '名称'},
          {id: 'topics_num', name: '题量'},
          {id: 'count', name: '时长'},
          {id: 'summary', name: '简介'}
        ]
      }
    }
  },
  created () {
    this.tableList()
  },
  methods: {
    tableList () {
      APISINGLELIST({
        title: this.title
      }).then((res) => {
        if (res.status === 1) {
          this.tableData = res.data.list
          this.totalNumber = res.data.totalNumber
          this.toggleSelection(this.singleList)
        } else {
          this.$message.error(res.msg)
          this.tableData = []
          this.tableData.push(this.seacherTab)
        }
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    toggleSelection (rows) {
      if (rows) {
        this.$nextTick(() => {
          rows.forEach(row => {
            this.tableData.forEach((item, index) => {
              if (row.id === item.id) {
                this.$refs.multipleTable.toggleRowSelection(this.tableData[index])
              }
            })
          })
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange (val) {
      this.$emit('haveChosen', val.val, this.totalNumber)
    }
  }
}
</script>
